package com.tool.genxml.handler;

import com.tool.genxml.entity.MergedRegion;
import com.tool.genxml.entity.XRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.util.Map;
import java.util.Objects;

/**
 * @author: zhoubin
 * @date: 2024/3/28
 */
public abstract class SheetHandler extends MergedRegionsHandler {

    /**
     * 需要处理的Sheet页
     */
    private XSSFSheet sheet;

    /**
     * 是否含有组件
     */
    private boolean hasComponent = false;

    public SheetHandler(XSSFSheet sheet) {
        this.sheet = sheet;
    }

    /**
     * 获取sheet页名
     * @return
     */
    public String sheetName() {
        return sheet.getSheetName();
    }

    @Override
    public void exec() {
        System.out.println("解析" + sheet.getSheetName() + "页。");

        // 遍历sheet页中的所有合并单元格
        iterateMergedRegion(sheet);

        // 解析sheet页
        analysisSheet();

        // 装配组件
        if (hasComponent) {
            assembleComponent();
        }
    }

    public XSSFSheet getSheet() {
        return sheet;
    }

    public void setSheet(XSSFSheet sheet) {
        this.sheet = sheet;
    }

    public boolean isHasComponent() {
        return hasComponent;
    }

    public void setHasComponent(boolean hasComponent) {
        this.hasComponent = hasComponent;
    }

    protected XRow assembleXRow(XSSFRow row, Map<Integer, MergedRegion> rowRegions) {
        try {
            return assembleXRowInner(row, rowRegions);
        } catch (Exception e) {
            System.err.println("组装带有合并单元格的行失败行数为: " + row.getRowNum() + ", sheet页为: " + row.getSheet().getSheetName());
            throw e;
        }
    }
    /**
     * 组装带有合并单元格的行
     * @param row 当前行
     * @param rowRegions 当前行中对应的其他的合并单元格
     * @return
     */
    protected XRow assembleXRowInner(XSSFRow row, Map<Integer, MergedRegion> rowRegions) {
        XRow xRow = new XRow();
        // 获取报文要素
        String name = row.getCell(2).toString();
        xRow.setOriginName(name);
        // 获取注释
        String note = getSheet().getRow(row.getRowNum() + 1).getCell(2).toString();
        xRow.setNotes(note.trim());
        // tag
        xRow.setOriginTag(rowRegions.get(3).getVal());
        // 循环次数
        xRow.setOccurs(rowRegions.get(6).getVal());
        // 数据类型，有可能写的是组件类型
        MergedRegion dataTypeRegion = rowRegions.get(7);
        if (Objects.nonNull(dataTypeRegion)) {
            xRow.setDataType(dataTypeRegion.getVal());
        }

        // 备注，如果其中写的是组件类型
        MergedRegion markRegion = rowRegions.get(8);
        if (Objects.nonNull(markRegion)) {
            xRow.setMark(markRegion.getVal());
        } else { // 如果为空，则肯定为未合并的单元格，如果组件类型未写在数据类型中的话，则需要在其中寻找组件类型
            if (!xRow.isHasComponent()) {
                XSSFCell cell = row.getCell(8);
                xRow.setMark(cell);
            }
        }

        // 是否是加签要素
        MergedRegion signRegion = rowRegions.get(9);
        if (Objects.nonNull(signRegion)) {
            xRow.setOriginSign(signRegion.getVal());
        }

        // 设置默认值
        MergedRegion valRegion = rowRegions.get(10);
        if (Objects.nonNull(valRegion)) {
            xRow.setValue(valRegion.getVal());
        }

        return xRow;
    }
}
